$NG.AllReady(function (
	page,
	{
		useValuesChange,
		useClick,
		useUpdateRow,
		useUpdateRows,
		useDataIndexChange,
		useBeforeClick,
	}
) {
	var mstform = $NG.getCmpApi("p_form_ticket");
	var dgrid = $NG.getCmpApi("p_form_ticket_d"); //获取表体数据
	let user = $NG.getUser();
	/**
	 *@default: 初始化
	 */
	if ($NG.getCmpApi("p_form_ticket")) {
		cntMFilter();
	}
	/**
	 *@license://表头字段变化逻辑END	//表头字段变化逻辑BEGIN
	 */
	useValuesChange(({ args }) => {
		console.log(args, "u_cnt_name");
		console.log(args.values.u_cnt_name, "u_cnt_name");

		getCntMInfoByPhid();
	}, "p_form_ticket.u_cnt_name");
    useValuesChange(({args})=>{
        getPtInfoByPhid()
        cntMFilter()
    },"p_form_ticket.phid_pc")
	useUpdateRows(({ args }) => {
		let rows = args[0];
		let total = 0;
		rows.forEach((item) => {
			if (!item.u_fk_amt) return;
			total += item.u_fk_amt; // 确保值存在且为数字
		});
		console.log(total, "total");

		mstform.getItem("u_forfeit").setValue(total);
		// 更新和刷新表格
	}, "p_form_ticket_d");
	/**
	 * @function: 过滤合同类型函数
	 */
	function cntMFilter() {
		$NG.updateUI((updater, state) => {
			updater.fieldSetForm.p_form_ticket.u_cnt_name.setProps({
				onBeforeOpen: async () => {
                    if(!mstform.getItem("phid_pc").getValue()) {
                        $NG.alert("请选择项目")
                        return false
                    }
                    return true
                },
				...{
					clientSqlFilter: {
						bill_type: 7,
                        phid_pc:mstform.getItem("phid_pc").getValue()
					},
				},
			});
		});
	}
    getPtInfoByPhid()
    function getPtInfoByPhid() {
        if (mstform.getItem("phid_pc").getValue()) {
          $NG.execServer("getPtInfoByPhid", { phid: mstform.getItem("phid_pc").getValue() }, function (res) {
            const datas = JSON.parse(res.data)[0].extendObjects;
            console.log(datas);
            $NG.updateState((updater) => {
              updater.data.p_form_ticket.setProps({
                //项目编码
                u_pc_no: datas.pc_no,
              });
            });
          });
        }
      }
      


	function getCntMInfoByPhid(params) {
		$NG.execServer(
			"getCntMInfoByPhid",
			{ phid: mstform.getItem("u_cnt_name").getValue() },
			function (res) {
				const datas = JSON.parse(res.data)[0].extendObjects;
				console.log(datas, "data1");
				$NG.updateState((updater) => {
					updater.data.p_form_ticket.setProps({
						u_supplier: { value: datas.supply_id, label: datas.supply_name },
					});
				});
			}
		);
		$NG.execServer(
			"getPtInfoByPhid_cnt",
			{ phid_cnt: mstform.getItem("u_cnt_name").getValue() },
			function (res) {
				console.log(res.data, "getPtInfoByPhid_cnt");
				const datas = JSON.parse(res.data)[0].extendObjects;
				console.log(datas);
				$NG.updateState((updater) => {
					updater.data.p_form_ticket.setProps({
						//产品线
						u_product_line: {
							value: datas.midwife_phid,
							label: datas.midwife_name,
						},
						//乙方单位
						u_regional_center: {
							value: datas.regional_center_phid,
							label: datas.regional_center_name,
						},
						u_division: {
							value: datas.division_phid,
							label: datas.division_name,
						},
						u_cnt_no: datas.pc_no,
					});
				});
			}
		);
	}
});

// getItemInfoZZCP; 物料查询sql使用
